<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>首页 - 内容列表页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.8/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend> 搜索 </legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">栏目</label>
                            <div class="layui-input-inline" id="id_div_menu_select">
                                加载中...
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">类型</label>
                            <div class="layui-input-inline" id="id_div_type_select">
                                加载中...
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" placeholder="标题关键词" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>

                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    </div>
</div>
<script src="/lib/layui-v2.6.8/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table;

        $.get('/index/menuOptions', {}, function (re) {
            if (re.code == 0) {
                let select = '<select name="menu_id" class="layui-select"><option value="">- 全部 -</option>';
                select += '<option value="0">├── 未关联栏目</option>';
                for (let i in re.data) {
                    select += '<option value="' + re.data[i].id + '">' + re.data[i].name + '</option>';
                }
                select += '</select>';
                $('#id_div_menu_select').html(select);
                form.render();
            } else {
                layer.alert('栏目加载失败 !');
            }
        }, 'json');

        $.get('/index/itemTypes', {}, function (re) {
            if (re.code == 0) {
                let select = '<select name="type_id" class="layui-select"><option value="">- 全部 -</option>';
                for (let i in re.data) {
                    select += '<option value="' + i + '">' + re.data[i] + '</option>';
                }
                select += '</select>';
                $('#id_div_type_select').html(select);
                form.render();
            } else {
                layer.alert('类型加载失败 !');
            }
        }, 'json');

        table.render({
            elem: '#currentTableId',
            url: '/index/itemList',
            cols: [[
                {field: 'id', width: 65, title: 'ID'},
                {field: 'menu_name', width: 150, title: '栏目'},
                {field: 'type_name', width: 150, title: '类型'},
                {title: '标题', templet: function (row) {
                    if (row.title == '') {
                        row.title = '未填写标题';
                    }
                    let url = '/detail_' + row.type_id + '.html?id=' + row.id;
                    if (row.type_id == 0) {
                        url = '/detail/' + row.id + '.html';
                    }
                    return '<i class="layui-icon layui-icon-link"></i><a target="_blank" href="' + url + '">' + row.title + '</a>';
                }},
                {field: 'tag_names', title: '标签'},
                {field: 'created_at', width: 180, title: '创建时间'},
                {field: 'updated_at', width: 180, title: '最近更新'}
            ]],
            limits: [50, 100],
            limit: 50,
            page: true,
            skin: 'line'
        });

        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: {curr: 1},
                where: data.field
            });
            return false;
        });

    });
</script>

</body>
</html>